// 编写账单列表store

import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const billStore = createSlice({
    name:'bill',
     // 数据状态 
     initialState:{
       billList:[]
     },
     reducers:{
        // 同步修改方法
        setBillList(state,action){
             state.billList = action.payload 
        },
        addBill(state,action){
            state.billList.push(action.payload)
        }
     }
})

// 解构actionCreater函数
 const {  setBillList,addBill }  = billStore.actions

// 编写异步
const getBillList = () => {
    return async (dispatch) => {
        //编写异步请求
        // 触发同步reducer
      const list =  await axios.get("http://localhost:3004/ka");
      dispatch(setBillList(list.data))
    }
}

const addBillData = (data) => {
    return async (dispatch) => {
        const res = await axios.post('http://localhost:3004/ka',data);
        dispatch(addBill(res.data))
    }
}

export { getBillList,addBillData } 

 // 导出reducer
  const reducer = billStore.reducer;
  

  export default reducer; 
 